package com.monitoring.interfaces.controller;

import com.monitoring.application.dto.EnvironmentalDataDTO;
import com.monitoring.application.service.EnvironmentalDataApplicationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/environmental-data")
public class EnvironmentalDataController {

    private final EnvironmentalDataApplicationService environmentalDataApplicationService;

    @Autowired
    public EnvironmentalDataController(EnvironmentalDataApplicationService environmentalDataApplicationService) {
        this.environmentalDataApplicationService = environmentalDataApplicationService;
    }

    // 记录新的环境数据
    @PostMapping
    public ResponseEntity<EnvironmentalDataDTO> recordEnvironmentalData(@RequestBody EnvironmentalDataDTO dataDTO) {
        EnvironmentalDataDTO recordedData = environmentalDataApplicationService.recordEnvironmentalData(dataDTO);
        return new ResponseEntity<>(recordedData, HttpStatus.CREATED);
    }

    // 根据传感器ID获取环境数据
    @GetMapping("/sensor/{sensorId}")
    public ResponseEntity<List<EnvironmentalDataDTO>> getBySensorId(@PathVariable Long sensorId) {
        List<EnvironmentalDataDTO> dataList = environmentalDataApplicationService.getEnvironmentalDataBySensorId(sensorId);
        return ResponseEntity.ok(dataList);
    }

    // 根据位置获取环境数据
    @GetMapping("/location/{location}")
    public ResponseEntity<List<EnvironmentalDataDTO>> getByLocation(@PathVariable String location) {
        List<EnvironmentalDataDTO> dataList = environmentalDataApplicationService.getEnvironmentalDataByLocation(location);
        return ResponseEntity.ok(dataList);
    }

    // 根据时间范围获取环境数据
    @GetMapping("/time-range")
    public ResponseEntity<List<EnvironmentalDataDTO>> getByTimeRange(
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime startTime,
            @RequestParam @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME) LocalDateTime endTime) {
        List<EnvironmentalDataDTO> dataList = environmentalDataApplicationService.getEnvironmentalDataByTimeRange(startTime, endTime);
        return ResponseEntity.ok(dataList);
    }

    // 获取传感器的最新数据
    @GetMapping("/sensor/{sensorId}/latest")
    public ResponseEntity<List<EnvironmentalDataDTO>> getLatestBySensorId(@PathVariable Long sensorId) {
        List<EnvironmentalDataDTO> latestData = environmentalDataApplicationService.getLatestEnvironmentalData(sensorId, null, 1);
        return ResponseEntity.ok(latestData);
    }

    // 获取传感器的特定类型最新数据
    @GetMapping("/sensor/{sensorId}/latest/{dataType}")
    public ResponseEntity<List<EnvironmentalDataDTO>> getLatestBySensorAndType(@PathVariable Long sensorId, @PathVariable String dataType) {
        List<EnvironmentalDataDTO> latestData = environmentalDataApplicationService.getLatestEnvironmentalData(sensorId, dataType, 1);
        return ResponseEntity.ok(latestData);
    }

    // 处理高温情况
    @PostMapping("/handle-high-temperature")
    public ResponseEntity<Void> handleHighTemperature(
            @RequestParam Long sensorId,
            @RequestParam Double temperature) {
        environmentalDataApplicationService.handleHighTemperature(sensorId, temperature);
        return ResponseEntity.ok().build();
    }

    // 计算能耗成本
    @GetMapping("/energy-cost/{deviceId}")
    public ResponseEntity<Map<String, Object>> calculateEnergyCost(
            @PathVariable Long deviceId,
            @RequestParam(defaultValue = "1.0") Double pricePerKwh) {
        double cost = environmentalDataApplicationService.calculateEnergyCost(deviceId, pricePerKwh);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("deviceId", deviceId);
        result.put("cost", cost);
        return ResponseEntity.ok(result);
    }
}